package com.study.talk.ui.app.change

import android.app.DatePickerDialog
import android.net.Uri
import android.os.Bundle
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.databinding.DataBindingUtil
import com.study.talk.R
import com.study.talk.databinding.ActivityChangeUserInfoBinding
import com.study.talk.ui.app.base.BaseActivity
import com.study.talk.utils.FileUtil
import com.study.talk.utils.extras.showToast
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class ChangeUserInfoActivity : BaseActivity() {

    private val takePhoto =
        registerForActivityResult(ActivityResultContracts.TakePicture()) { result: Boolean ->
            if (result) {
                uri?.let { vm.upload(FileUtil.getPathFromUri(this, it)) }
            }
        }
    private val selectPhoto =
        registerForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri: Uri? ->
            uri?.let { vm.upload(FileUtil.getPathFromUri(this, uri)) }
        }

    private val binding: ActivityChangeUserInfoBinding by lazy {
        DataBindingUtil.setContentView(
            this@ChangeUserInfoActivity, R.layout.activity_change_user_info
        )
    }

    private val vm: ChangeUserInfoViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setSupportActionBar(binding.changeUserInfoToolbar)
        vm.loadUserInfo()
        binding.lifecycleOwner = this
        binding.vm = vm
        initView()
    }


    private fun initView() {

        binding.changeUserInfoToolbar.setNavigationOnClickListener { finish() }

        vm.gender.observe(this) {
            if (it == 1) {
                binding.genderGroup.check(binding.changeUserInfoManBt.id)
            } else {
                binding.genderGroup.check(binding.changeUserInfoWomanBt.id)
            }
        }

        binding.genderGroup.setOnCheckedChangeListener { group, checkedId ->
            if (group.checkedRadioButtonId != checkedId) {
                group.check(checkedId)
            }
            if (checkedId == binding.changeUserInfoManBt.id) {
                vm.gender.postValue(1)
            } else {
                vm.gender.postValue(0)
            }
        }

        vm.complete.observe(this) { info ->
            info?.let {
                showToast("信息修改成功！")
                app.updateUserInfo(it)
            }
        }

        binding.changeUserInfoImg.setOnClickListener {
            showSelectPhotoDialog(
                selectPhoto, takePhoto
            )
        }

        binding.changeUserInfoBirth.setOnClickListener {
            val dialog = DatePickerDialog(this)
            dialog.show()
            dialog.setOnDateSetListener { _, year: Int, month: Int, day: Int ->
                vm.changeBirth(year, month, day)
            }
        }

        binding.changeUserInfoBt.setOnClickListener {
            if (vm.check()) {
                vm.submit()
            }
        }
    }
}